<?php
session_start();
include 'nav.inc.php';
html_head('Stundenübersicht');
include 'dbConn.php';
if(!isset($_SESSION["username"])){
	echo("</head>");
	echo("<body>");
	echo("<div class='content'>");
	echo ("Bitte erst <a href='../login.php'>einloggen</a>.");
	exit;
	echo("</div>");
	echo("</body>");
	echo("</html>");
}
else{
	echo("<link rel='stylesheet' type='text/css' href='tcal.css' />");
	echo("<script type='text/javascript' src='tcal.js'></script> ");
	echo("<script type='text/javascript' src='clone_this.js'></script> ");
	echo("</head>");
	echo("<body>");
	menu();
	echo("<div class='content'>");

	if (isset($_REQUEST['jahr']))
		$jahr = $_REQUEST['jahr'];
	else
		$jahr = date("Y");
	echo ("<form action='".$_SERVER['SCRIPT_NAME']."' method='post'>\n"); 
	echo ("<select name='jahr' size='1'>\n");

	for ($tmp = date("Y") ; $tmp<=date("Y") +2; $tmp++){
		if ($tmp == $jahr)
			echo ("<option selected='selected'>$tmp</option>\n");
		else
			echo ("<option>$tmp</option>\n");
	}

	echo ("</select>\n");
	echo ("<input type='submit' value='Ausw&auml;hlen'>\n");
	echo ("</form>\n");
	echo("<br/>");
	if (isset($_REQUEST['speichern'])){
		for ($tmp=1; $tmp<=12; $tmp++){
			$arbeitstage = $_REQUEST["monat".$tmp];
			$sql = "SELECT Arbeitstage FROM Arbeitstage WHERE Monat = '$tmp' AND Jahr = '$jahr'";
			$result = mysql_query($sql) or die (mysql_error());
			$row = mysql_fetch_assoc($result);
	
			if (empty($row['Arbeitstage']) && $arbeitstage != 0)
				$sql = "INSERT INTO Arbeitstage VALUES ('$tmp', '$jahr', '$arbeitstage')";
			else if ($arbeitstage != 0)
				$sql = "UPDATE Arbeitstage SET Arbeitstage = '$arbeitstage' WHERE Monat = '$tmp' AND Jahr = '$jahr'";
			else
				continue;
	
			//echo ("$sql<br>");
			mysql_query($sql) or die (mysql_error());
		}
	
		echo ("<div style='color:green;'>Erfolgreich eingetragen!</div>\n");
	}
	echo ("<form action='".$_SERVER['SCRIPT_NAME']."' method='post'>\n"); 
	echo ("<table>\n");	
	for ($tmp=1; $tmp<=12; $tmp++){
		echo ("<tr>");
		echo ("<td>".date("M", mktime(0, 0, 0, $tmp))."</td>");
		echo ("<td>".date("Y", mktime(1, 1, 1, 1, 1, $jahr))."</td>");
		$sql = "SELECT Arbeitstage FROM Arbeitstage WHERE Monat = '$tmp' AND Jahr = '$jahr'";
		$result = mysql_query($sql) or die (mysql_error());
		$row = mysql_fetch_assoc($result);

		if (empty($row['Arbeitstage']))
			echo ("<td><input type='text' name='monat".$tmp."' size='2' maxlength='2' value='0' style='text-align:center;'></td></tr>\n");
		else
			echo ("<td><input type='text' name='monat".$tmp."' size='2' maxlength='2' value='".$row['Arbeitstage']."' style='text-align:center;'></td></tr>\n");

		mysql_free_result($result);
	}

	echo ("</table>\n");	
	echo ("<input type='submit' name='speichern' value='Speichern'>\n");
	echo ("<input type='hidden' name='jahr' value='$jahr'>\n");
	echo ("</form>\n");
	echo("</div>");
}
?>

</body>
</html>
